package QianFeng15;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 注解
 **/
@SuppressWarnings("all")//消除所有类型的警告信息
public class AnnotationDemo {
    public static void main(String[] args) {
        Cat cat1 = new Cat("小明", 12);
        Cat cat2 = new Cat("小黄", 13);
        cat1.printInfo();//方法已经过时了 发出警告

        //下面的警告被清除了
        List list = new ArrayList();
        list.add(cat1);
        list.add(12);
        list.add("夏明");


    }

    @Test
    public void Test() {
        /*反射处理注解*/
        Class<Cat> catClass = Cat.class;
        //获取类上应用的指定注解
        MyAnnotation annotation = catClass.getAnnotation(MyAnnotation.class);
        //获取注解上变量的值，没有获取默认值
        String name = annotation.name();
        int age = annotation.age();
        Color color = annotation.color();
        String[] like = annotation.like();
        try {
            Cat cat = catClass.newInstance();
            cat.setName(name);
            cat.setAge(age);
            cat.setColor(color);
            cat.setLike(like);
            System.out.println(cat.toString());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
